package com.wlc.controller.music;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;



/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description
 * @date 2021-08-14 10:49
 */
public class HttpGetConnect {
    /**
     *  获取html内容
     * @param url
     * @param charsetName  UTF-8、GB2312
     * @return
     * @throws IOException
     */
    public  String connect(String url,String charsetName) throws IOException {
        BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();

        CloseableHttpClient httpclient = HttpClients.custom()
                .setConnectionManager(connManager)
                .build();
        String content = "";

        try{
            HttpGet httpget = new HttpGet(url);

            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(5000)
                    .setConnectTimeout(50000)
                    .setConnectionRequestTimeout(50000)
                    .build();
            httpget.setConfig(requestConfig);
//            httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
//            httpget.setHeader("Accept-Encoding", "gzip,deflate,sdch");
//            httpget.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
//            httpget.setHeader("Connection", "keep-alive");
//            httpget.setHeader("Upgrade-Insecure-Requests", "1");
//            httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
//            //httpget.setHeader("Hosts", "www.oschina.net");
//            httpget.setHeader("cache-control", "max-age=0");

            httpget.setHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            httpget.setHeader("accept-encoding", "gzip, deflate, br");
            httpget.setHeader("accept-language", "zh-CN,zh;q=0.9");
            httpget.setHeader("cache-control", "max-age=0");
            httpget.setHeader("cookie", "kg_mid=798abf7efd9a07fd9e92b409ca2be1e1; kg_dfid=3Mnlhg3uPvww07Ydxn1jYonP; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1628596533,1628597491");
            httpget.setHeader("if-modified-since", "Wed, 11 Aug 2021 01:24:11 GMT");
            httpget.setHeader("sec-ch-ua", "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\"");
            httpget.setHeader("sec-ch-ua-mobile", "?0");
            httpget.setHeader("sec-fetch-dest", "document");
            httpget.setHeader("sec-fetch-mode", "navigate");
            httpget.setHeader("sec-fetch-site", "none");
            httpget.setHeader("sec-fetch-user", "?1");
            httpget.setHeader("upgrade-insecure-requests", "1");
            httpget.setHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36");



            CloseableHttpResponse response = httpclient.execute(httpget);

            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {

                HttpEntity entity = response.getEntity();
                InputStream instream = entity.getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(instream,charsetName));
                StringBuffer sbf = new StringBuffer();
                String line = null;
                while ((line = br.readLine()) != null){
                    sbf.append(line + "\n");
                }

                br.close();
                content = sbf.toString();
            } else {
                content = "";
            }

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            httpclient.close();
        }
        //log.info("content is " + content);
        return content;
    }
    private static Log log = LogFactory.getLog(HttpGetConnect.class);
}

